import {
  Entity, PrimaryGeneratedColumn, Column
} from 'typeorm';
import NodeAuth from 'node-auth0';
import { Exclude, Expose } from 'class-transformer';

@Entity({ name: 'struct' })
export class StructEntity {
  @Exclude()
  private nodeAuth: NodeAuth;

  constructor() {
    this.nodeAuth = new NodeAuth();
  }

  @PrimaryGeneratedColumn({
    comment: '主键id',
    name: 'id',
    type: 'int',
  })
  id: number;

  @Column('varchar', {
    nullable: false,
    primary: true,
    generated: 'uuid',
    length: 50,
    name: 'uuid',
    comment: 'uuid',
  })
  uuid: string;

  @Column('int', {
    nullable: false,
    name: 'pid',
    default: 0,
    comment: '上级机构',
  })
  pid: number;

  @Column('varchar', {
    nullable: false,
    length: 100,
    name: 'name',
    comment: '组织机构名称',
  })
  name: string;

  @Column('varchar', {
    nullable: false,
    length: 150,
    name: 'area',
    comment: '归属区域',
  })
  area: string;

  @Column('int', {
    nullable: false,
    default: () => 10000,
    name: 'code',
    comment: '机构编码',
  })
  code: number;

  @Column('tinyint', {
    nullable: true,
    default: 1,
    name: 'type',
    comment: '机构类型',
  })
  type: number | null;

  @Column('tinyint', {
    nullable: true,
    default: 1,
    name: 'status',
    comment: '机构状态',
  })
  status: number | null;

  @Column('varchar', {
    nullable: true,
    name: 'detail',
    comment: '详细地址',
  })
  detail: string;

  @Column('varchar', {
    nullable: true,
    length: 150,
    name: 'note',
    comment: '备注',
  })
  note: string;

  @Column('timestamp', {
    nullable: false,
    default: () => 'CURRENT_TIMESTAMP',
    name: 'createdAt',
    comment: '创建时间',
  })
  createdAt: Date;

  @Column('timestamp', {
    nullable: false,
    default: () => 'CURRENT_TIMESTAMP',
    name: 'updatedAt',
    comment: '最后更新时间',
  })
  updatedAt: Date;

  @Expose() //根据现有字段生成一个新的字段
  get is_active(): string {
    return this.status ? '启用' : '禁用';
  }

}
